package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.Bitmap;

class HomeScreen extends MainScreen  {
    
    private String mnuArtists;
    private String mnuAlbums;
    private String mnuPlaylists;
    private String mnuTags;
    private String mnuSearch;
    private String mnuList;
    private String[] mainMenuItems;
    private MyObjectListField menu;
    private ArtistsScreen artistsScreen;
    private AlbumsScreen albumsScreen;
    private PlaylistsScreen playlistsScreen;
    private TagsScreen tagsScreen;
    private SearchScreen searchScreen;
    private CurrentPlaylistScreen currPlaylistScreen;
    private OptionsScreen optionsScreen;
    
    private MenuItem optionsMenu = new MenuItem("Options",100,10) {
       public void run() {
           showOptionsScreen();
       } 
    };
    
    HomeScreen() {
    	//create drawMenu separately below in order to callback
    	//to HomeScreen when OptionsScreen saves changes
        this.setTitle("Ampacheberry");
    	drawMenu();
    }
       
    
    //Create drawMenu separately in order to callback
    //from OptionsScreen when changes are saved
    public void drawMenu() {
        menu = new MyObjectListField();
        this.deleteAll();
        
        if (ampacheberry.comm.authToken.equals("") || 
        		ampacheberry.comm.authToken == null) {
        	add(new RichTextField("Not Connected to Server!!"));
         }
        else
        {        	
            mnuArtists = "Artists" + " (" + ampacheberry.comm.artists + ")";
            mnuAlbums = "Albums" + " (" + ampacheberry.comm.albums + ")";
            mnuPlaylists = "Playlists" + " (" + ampacheberry.comm.playlists + ")";
            mnuTags = "Tags" + " (" + ampacheberry.comm.tags + ")";
            mnuSearch = "Search";
            mnuList = "Current Playlist";
            mainMenuItems = new String[]{mnuArtists,mnuAlbums,mnuPlaylists,mnuTags, mnuSearch, mnuList};
            menu.set(mainMenuItems);
            this.add(menu);
        }
    }
    
	
    public void showOptionsScreen() {
        if (null == optionsScreen) {
            optionsScreen = new OptionsScreen();
        }
        optionsScreen.displayOptions();
        UiApplication.getUiApplication().pushModalScreen(optionsScreen);
    }
    
    
    protected void makeMenu(Menu menu, int instance) {
        menu.add(optionsMenu);
        menu.add(MenuItem.separator(optionsMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
    
    
    protected boolean navigationClick(int status, int time) {
        
        // Determine which menu item was clicked.
        int selected = menu.getSelectedIndex();
        switch (selected) {
            case 0:
                if (null == artistsScreen) {
                 artistsScreen = new ArtistsScreen();   
                }
                UiApplication.getUiApplication().pushScreen(artistsScreen);
                artistsScreen.getArtists();
            break;
            case 1:
                if (null == albumsScreen) {
                 albumsScreen = new AlbumsScreen();   
                }
                UiApplication.getUiApplication().pushScreen(albumsScreen);
                albumsScreen.getAlbums();
            break;
            case 2:
                if (null == playlistsScreen) {
                	playlistsScreen = new PlaylistsScreen();
                }
                UiApplication.getUiApplication().pushScreen(playlistsScreen);
                playlistsScreen.getPlaylists();
            break;
            case 3:
                if (null == tagsScreen) {
                	tagsScreen = new TagsScreen();
                }
                UiApplication.getUiApplication().pushScreen(tagsScreen);
                tagsScreen.getTags();
            break;
            case 4:
                searchScreen = new SearchScreen();
                UiApplication.getUiApplication().pushScreen(searchScreen);
            break;
            case 5:
            	if (null == currPlaylistScreen) {
            		currPlaylistScreen = new CurrentPlaylistScreen();
            	}
            	UiApplication.getUiApplication().pushScreen(currPlaylistScreen);
            	currPlaylistScreen.getList();
            break;
        }
        
        return true;
    }
    
    
    private class MyObjectListField extends ObjectListField {
        
        private Bitmap artistIcon = Bitmap.getBitmapResource("img/artist.png");  
        private Bitmap albumIcon = Bitmap.getBitmapResource("img/album.png");
        private Bitmap tagIcon = Bitmap.getBitmapResource("img/tag.png");
        private Bitmap playlistIcon = Bitmap.getBitmapResource("img/playlist.png");
        private Bitmap searchIcon = Bitmap.getBitmapResource("img/search.png");
        private Bitmap icon;
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {
        	
            if ( mainMenuItems[index] == mnuArtists) {
                icon = artistIcon;
            }   
            if ( mainMenuItems[index] == mnuAlbums) {
                icon = albumIcon;
            }  
            if ( mainMenuItems[index] == mnuTags) {
                icon = tagIcon;
            }
            if ( mainMenuItems[index] == mnuPlaylists) {
                icon = playlistIcon;
            }
            if ( mainMenuItems[index] == mnuSearch) {
                icon = searchIcon;
            }
            if ( mainMenuItems[index] == mnuList) {
                icon = playlistIcon;
            }
        	
            if (null != icon) {
                int offsetY = (this.getRowHeight() - icon.getHeight())/2;
                graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                        icon.getHeight(),icon,0,0);
                graphics.drawText(mainMenuItems[index], 
                    icon.getWidth() + 2, y, DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            } else {
                graphics.drawText("- " + mainMenuItems[index], 0,
                    y, DrawStyle.ELLIPSIS, width - graphics.getFont().getAdvance("- "));
            }
        }
    };
} 
